package webs;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.pojo.Files;
import domain.ApplicationContext;
import domain.FileFacade;

/**
 * Created by IntelliJ IDEA.
 * User: winstars
 * Date: 2007-3-1
 * Time: 12:06:06
 * To change this template use File | Settings | File Templates.
 */
public class GetFile extends HttpServlet{
	
    private FileFacade fileService;


    public FileFacade getFileService() {
        return fileService;
    }

    public void setFileService(FileFacade fileService) {
        this.fileService = fileService;
    }
    
    @Override
    public void destroy() {
        super.destroy();   
    }

    @Override
    public void init() throws ServletException {
        super.init();   
    }
	
	 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	    {
		    request.setCharacterEncoding("UTF-8");
		    fileService=(FileFacade)ApplicationContext.get("FileService");
		    
		    Integer file_id = new Integer(request.getParameter("file_id"));
            Files file=new Files();
            file.setId(file_id);
            file=fileService.getFilesById(file);  //获取文件信息
            
	   	    response.setContentType("application/octet-stream");
	   	    response.setHeader("Content-Disposition", "attachment; filename="+new String(file.getFileName().getBytes("GBK"),"ISO8859-1"));
	   	    ServletOutputStream out = response.getOutputStream();
	   	    fileService.downFiles(file, out);
	   	    //response.sendRedirect("")
	    }
}


